<?php
/**
 * 常用GD函数
 * 20150906
 * @author YUE
 */
class Gdfuncs{
	
	/**
	 * 根据SRC取得图片资源
	 * 只支持JPG、PNG
	 */
	public static function imagecreatefromsrc($src){
		$ext = strtolower(strrchr($src, "."));
		switch($ext){
			case ".jpg":
			case ".jpeg":
				$resource = imagecreatefromjpeg($src);break;
			case ".png" :
				$resource = imagecreatefrompng($src);break;
			default :
				$resource = null;
		}
		return $resource;
	}
	
	/**
	 * 指定宽度等比缩放
	 * 输入RES,返回也是RES
	 */
	public static function resizeConstantRateRes($resource, $new_width){
		
		$ori_resource = $resource;
		if(empty($ori_resource))return false;
		$ori_width = imagesx($ori_resource);
		$ori_height = imagesy($ori_resource);
		$extname = substr($src, strrpos($src, "."));
		
//		echo "ori_width:".$ori_width."<br>";
//		echo "ori_height:".$ori_height."<br>";
//		echo "extname:".$extname."<br>";
		
		$whrate = $ori_width/$ori_height;//宽高比
		$new_height = intval($new_width/$whrate);
//		echo "whrate:".$whrate."<br>";
//		echo "new_width:".$new_width."<br>";
//		echo "new_height:".$new_height."<br>";
		
		if(function_exists("imagecopyresampled")){
			$new_resource = imagecreatetruecolor($new_width, $new_height);
			imagecopyresampled($new_resource,$ori_resource,0,0,0,0,$new_width,$new_height,$ori_width,$ori_height);
		}else{
			$new_resource = imagecreate($new_width,$new_height);
			imagecopyresized($new_resource,$ori_resource,0,0,0,0,$new_width,$new_height,$ori_width,$ori_height);
		}
		imagedestroy($ori_resource);
		return $new_resource;
	}
	
    /**
     * 图片剪裁，输入起始坐标和结束坐标，返回以两点为对角的矩形内的图像
     * @param $res
     * @param $x1
     * @param $y1
     * @param $x2
     * @param $y2
     * @return resource
     */
    public static function imageCut($res, $x1, $y1, $x2, $y2){
        //首先把坐标统一为左上和右下的对角坐标
        $_x1 = min($x1, $x2);
        $_y1 = min($y1, $y2);
        $_x2 = max($x1, $x2);
        $_y2 = max($y1, $y2);

        //新图宽高
        $new_width = $_x2 - $_x1;
        $new_height = $_y2 - $_y1;

        //开始剪裁
        if(function_exists("imagecopyresampled")){
            $new_resource = imagecreatetruecolor($new_width, $new_height);
            imagecopyresampled($new_resource,$res,0,0,$_x1,$_y1,$new_width,$new_height,$new_width,$new_height);
        }else{
            $new_resource = imagecreate($new_width,$new_height);
            imagecopyresized($new_resource,$res,0,0,$_x1,$_y1,$new_width,$new_height,$new_width,$new_height);
        }

        imagedestroy($res);
        return $new_resource;
    }
	
	/**
	 * 指定宽度等比缩放
	 * 输入SRC 返回RES
	 */
	public static function resizeEqualRateWidth($src, $new_width){
		$resource = GDfuncs::imagecreatefromsrc($src);
		return self::resizeConstantRateRes($resource, $new_width);
	}

	/**
	 * 指定宽度等比缩放
	 * 只支持JPG、PNG
	 */
	public static function resizeConstantRate($src, $width){
		$ext = strtolower(strrchr($src, "."));
		if(!in_array($ext, array('.jpg', '.jpeg', '.png')))return false;

		$resource = self::imagecreatefromsrc($src);
		if(empty($resource))return false;
		
		$new_resource = self::resizeConstantRateRes($resource, $width);
		if(empty($new_resource))return false;

		if(self::outImage($new_resource, $src))return true;
		else return false;
	}

	/**
	 * 生成图片(只支持JPG、PNG)
	 * 输入图片资源和路径，生成图片，比如输入 d:/abc/test.jpg 则在d:/abc下生成图片test.jpg(若目录不存在则报错)
	 * @author YUE
	 * @date 20160906
	 */
	public static function outImage($resource, $src){
		$ext = strtolower(strrchr($src, "."));
		switch($ext){
			case ".jpg":
			case ".jpeg":
				imagejpeg($resource, $src);break;
			case ".png" :
				imagepng($resource, $src);break;
			default :
				return false;
		}
		imagedestroy($resource);
		return true;
	}
	
	/**
	 * 加水印风格1(加在原图的右下角)
	 * 调用示例
	 * $res_dst = imagecreatefromjpeg("01.jpg");
	 * GDFuncs::water1($res_dst, imagecreatefrompng("shike.png"));
	 * imagejpeg($res_dst, 'out2.jpg');
	 * imagedestroy($res_dst); 
	 */
	public static function water1($ori_resource, $wat_resource){
		
		$ori_x = imagesx($ori_resource);
		$ori_y = imagesy($ori_resource);
		$wat_x = imagesx($wat_resource);
		$wat_y = imagesy($wat_resource);
		
		if($ori_x > $wat_x){
			imagecopy($ori_resource, $wat_resource, $ori_x-$wat_x, $ori_y-$wat_y, 0, 0, $wat_x, $wat_y);
			imagedestroy($wat_resource);
		}
	}
	
	/**
	 * 加水印风格1(加在原图的右下角)
	 * 调用示例
	 * $res_dst = imagecreatefromjpeg("01.jpg");
	 * GDFuncs::water1($res_dst, imagecreatefrompng("shike.png"));
	 * imagejpeg($res_dst, 'out2.jpg');
	 * imagedestroy($res_dst); 
	 */
	public static function water1src($ori_src, $wat_src){
		$ori_resource = self::imagecreatefromsrc($ori_src);
		$wat_resource = self::imagecreatefromsrc($wat_src);
		
		$ori_x = imagesx($ori_resource);
		$ori_y = imagesy($ori_resource);
		$wat_x = imagesx($wat_resource);
		$wat_y = imagesy($wat_resource);
		
		if($ori_x > $wat_x){
			imagecopy($ori_resource, $wat_resource, $ori_x-$wat_x, $ori_y-$wat_y, 0, 0, $wat_x, $wat_y);
			self::outImage($ori_resource, $ori_src);
			imagedestroy($wat_resource);
		}

	}
	
	/**
	 * 输入图片路径如果图片被旋转了给它校正（一般IOS照的相片会有这个问题）
	 */
	public static function rotate($src){
		$ext = strtolower(strrchr($src, "."));
		
		if(in_array($ext, array('jpg', 'jpeg', 'png')))return false;
		
		$exif = exif_read_data($src, 'IFD0');
		if($exif===false)return false;
		
		$exif = exif_read_data($src, 0, true);
	//	echo "<pre>";print_r($exif);echo "</pre>";
	
		$res = self::imagecreatefromsrc($src);
		
		if(isset($exif['IFD0']['Orientation']) && !empty($exif['IFD0']['Orientation'])) {
			$orientation = $exif['IFD0']['Orientation'];
			switch($orientation) {
			case 8:
				$res = imagerotate($res,90,0);
				break;
			case 3:
				$res = imagerotate($res,180,0);
				break;
			case 6:
				$res = imagerotate($res,-90,0);
				break;
			default:
				return false;
			}

			return self::outImage($res, $src);
		}else{
			return false;	
		}
		
	}
	
}

/*
$resource = GDfuncs::resizeEqualRateWidth('01.png', 650);

if($resource){
	imagejpeg($resource, 'out.jpg');
//	imagepng($resource, 'out.png');
	imagedestroy($resource);
}else{
	echo "error";	
}

*/




?>